package veterinaria.manager;

import geral.manager.GenericManager;
import geral.manager.UtilManager;
import geral.util.FacesUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import br.com.sgc.entities.veterinaria.Raca;
import br.com.sgc.session.GenericSession;

@Named
@ConversationScoped
public class RacaManager extends GenericManager implements Serializable{
	
	private static final long serialVersionUID = -439692882151383136L;

	@Inject private UtilManager utilManager;
	
	@EJB private GenericSession<Raca> racaSession;
	
	private Raca raca = new Raca();
	
	private List<Raca> racas = new ArrayList<Raca>();
	
	private Boolean showPanelCadastro;

	
	public String list(){
		racas = racaSession.findAll(Raca.class, utilManager.getSegurancaManager().getUsuarioLogado().getClinica().getId());
		showPanelCadastro = false;
		return "formRaca";
	}
	
	public String prepareSave(){
		showPanelCadastro = true;
		raca = new Raca();
		return "formRaca";
	}
	
	public String save(){
		try {
			raca.setClinica(utilManager.getSegurancaManager().getUsuarioLogado().getClinica());
			racaSession.save(raca);
			FacesUtil.mensInfo("Raça salva com sucesso");
			raca = new Raca();
			return list();
		} catch (Exception e) {
			FacesUtil.mensErro(e.getMessage());
			return null;
		}
	}
	
	public String prepareUpdate(){
		raca = racaSession.findById(Raca.class, raca.getId(), true);
		showPanelCadastro = true;
		return "formRaca";
	}
	
	public String update(){
		try {
			raca.setClinica(utilManager.getSegurancaManager().getUsuarioLogado().getClinica());
			racaSession.update(raca);
			raca = new Raca();
			FacesUtil.mensInfo("Espécie atualizado com sucesso");
			return list();
		} catch (Exception e) {
			FacesUtil.mensErro(e.getMessage());
			return null;
		}
	}

	public GenericSession<Raca> getRacaSession() {
		return racaSession;
	}

	public void setRacaSession(GenericSession<Raca> racaSession) {
		this.racaSession = racaSession;
	}

	public Raca getRaca() {
		return raca;
	}

	public void setRaca(Raca raca) {
		this.raca = raca;
	}

	public List<Raca> getRacas() {
		return racas;
	}

	public void setRacas(List<Raca> racas) {
		this.racas = racas;
	}

	public Boolean getShowPanelCadastro() {
		return showPanelCadastro;
	}

	public void setShowPanelCadastro(Boolean showPanelCadastro) {
		this.showPanelCadastro = showPanelCadastro;
	}
}
